﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSYSTEM
{
    public partial class FMI_TC_CABWARRING : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();
        private int iGridView = 0;

        public FMI_TC_CABWARRING()
        {
            InitializeComponent();
        }

        //查询代码
        protected override void OnSys04()
        {
            string sDate1 = uiDate1.Text;
            string sDate2 = uiDate2.Text;
            string SQL = "select fwarringdate,fwarringno,b.fpoint_name,g.fequipmenttag fconname"
               + " ,c.fequipmenttag fcabname,d.fequipmenttag fboxname"
               + " , e.fairname,fcurnum,fdescribe,i.flevelname "
               + "  from i_tc_cabwarring a"
               + "  left join a_ts_point b on a.fpoint_no = b.fpoint_no"
               + "  left join i_ts_cabinet c on c.fequipmentbarcode = a.fcabbarcode"
               + "  left join i_ts_warehouse d on d.fequipmentbarcode = a.fboxbarcode"
               + "  left join s_ts_airitem e on e.fairitem = a.fairitem"
               + "  left join i_ts_controlbox f on f.fcatbarcode=c.fequipmentbarcode"
               + "  left join i_ts_box g on g.fequipmentbarcode=f.fequipmentbarcode"
               + "  left join s_ts_danger_level i on i.flevelno = a.flevelno"
               + "  where 1=1  "
               + "  and convert(varchar(10),fwarringdate,120) between '"+sDate1+"'"
               + "  and '"+sDate2+"'";
            DataRowView dr1 = (DataRowView)cbfairitem.SelectedItem;
            if ((dr1 != null) && (cbfairitem.SelectedIndex > 0))
            {
                SQL = SQL + " and a.fairitem=" + dr1.Row["fairitem"].ToString();
            }

            DataRowView dr2 = (DataRowView)cbflevelno.SelectedItem;
            if ((dr2 != null) && (cbflevelno.SelectedIndex > 0))
            {
                SQL = SQL + " and a.flevelno='" + dr2.Row["flevelno"].ToString() + "'";
            }

            DataRowView dr3 = (DataRowView)cbfpoint_no.SelectedItem;
            if ((dr3 != null) && (cbfpoint_no.SelectedIndex > 0))
            {
                SQL = SQL + " and b.fpoint_no='" + dr3.Row["fpoint_no"].ToString() + "'";
            }

            DataRowView dr4 = (DataRowView)uibox.SelectedItem;
            if ((dr4 != null) && (uibox.SelectedIndex > 0))
            {
                SQL = SQL + " and g.fequipmentbarcode='" + dr4.Row["fequipmentbarcode"].ToString() + "'";
            }

            if (uicabbarcode.Text != "")
            {
                SQL = SQL + " and c.fequipmentbarcode='" + uicabbarcode.Tag.ToString() + "'";

            }
            SQL = SQL + "  order by a.fentryid";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();
            
            rowindex = -1;
        }

        private void FMI_TC_CABWARRING_Load(object sender, EventArgs e)
        {
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];

            //按钮控制
            sys01.Visible = false; sys01.Tag = "1";
            sys02.Visible = false; sys02.Tag = "1";
            sys03.Visible = false; sys03.Tag = "1";
            sys05.Visible = false; sys05.Tag = "1";
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys10.Visible = false; sys10.Tag = "1";
            sys11.Visible = false; sys11.Tag = "1";

            uiDate1.Text = DateTime.Now.ToString("yyyy-MM-dd");
            uiDate2.Text = DateTime.Now.ToString("yyyy-MM-dd");
            uicabbarcode.Tag = "";
            //下拉框
            DataTable myAirITEM = myCache.doGetDict("S_TS_AIRITEM", "");
            
            DataRow dr2 = myAirITEM.NewRow();
            dr2["fairname"] = "请选择";
            dr2["fairitem"] = "0";
            myAirITEM.Rows.InsertAt(dr2, 0);
            cbfairitem.DataSource = myAirITEM;
            cbfairitem.DisplayMember = "fairname";
            cbfairitem.ValueMember = "fairitem";
            cbfairitem.SelectedIndex = 0;

            DataTable myLevel = myCache.doGetDict("S_TS_DANGER_LEVEL", "");

            DataRow dr1 = myLevel.NewRow();
            dr1["flevelname"] = "请选择";
            dr1["flevelno"] = "0";
            myLevel.Rows.InsertAt(dr1, 0);
            cbflevelno.DataSource = myLevel;
            cbflevelno.DisplayMember = "flevelname";
            cbflevelno.ValueMember = "flevelno";
            cbflevelno.SelectedIndex = 0;

            DataTable myTs_Point = myCache.doGetDict("A_TS_POINT", "fpoint_flag=0");

            DataRow dr4 = myTs_Point.NewRow();
            dr4["fpoint_name"] = "请选择";
            dr4["fpoint_no"] = "0";
            myTs_Point.Rows.InsertAt(dr4, 0);
            cbfpoint_no.DataSource = myTs_Point;
            cbfpoint_no.DisplayMember = "fpoint_name";
            cbfpoint_no.ValueMember = "fpoint_no";
            cbfpoint_no.SelectedIndex = 0;

            DataTable myTs_BOX = StaticClass.myDB.query(" select fequipmentbarcode,fequipmenttag from i_ts_box");

            DataRow dr3 = myTs_BOX.NewRow();
            dr3["fequipmenttag"] = "请选择";
            dr3["fequipmentbarcode"] = "0";
            myTs_BOX.Rows.InsertAt(dr3, 0);
            uibox.DataSource = myTs_BOX;
            uibox.DisplayMember = "fequipmenttag";
            uibox.ValueMember = "fequipmentbarcode";
            uibox.SelectedIndex = 0;

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        private void cbfairitem_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void cbflevelno_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void uiBtn1_Click(object sender, EventArgs e)
        {
            //弹窗选择储物柜
            uicabbarcode.Text = "";
            uicabbarcode.Tag = "";

            string DATA = doPOPWINDOS("储物柜查询"
                , "fequipmentbarcode,储物柜编号,121;fequipmenttag,储物柜名称,141"
                , "select fequipmentbarcode,fequipmenttag,fmacno from i_ts_cabinet where 1=1"
                , " order by fequipmentbarcode"
                , 0);
            Console.WriteLine("当前返回:" + DATA);

            if ((DATA is null) || (DATA.Length == 0))
            {
                return;
            }
            string[] strArray = DATA.Split(',');

            uicabbarcode.Text = strArray[1];
            uicabbarcode.Tag = strArray[0];
        }

        private void cbfpoint_no_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void uibox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }
    }
}
